<template>
  <div>
    <FaPageMain>
      <el-button type="primary" @click="edit">新增</el-button>
      <!--    搜索框-->
      <div style="margin: 10px 0">
        <el-form :inline="true" :model="selectForm" class="demo-form-inline">
          <el-form-item label="名称">
            <el-input v-model="selectForm.name" placeholder="请输入配置名称" clearable/>
          </el-form-item>
          <el-form-item label="配置值">
            <el-input v-model="selectForm.value" placeholder="请输入配置值" clearable/>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="selectForm.description" placeholder="请输入描述" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="list">查询</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="编号"/>
        <el-table-column prop="name" label="标题"/>
        <el-table-column prop="value" label="值"/>
        <el-table-column prop="description" label="描述"/>
        <el-table-column prop="createdTime" label="创建时间"/>
        <el-table-column prop="updatedTime" label="更新时间"/>
        <el-table-column fixed="right" label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="primary" size="small" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="example-pagination-block">
        <el-pagination
          v-model:current-page="current"
          v-model:page-size="size"
          :page-sizes="[2, 5, 10, 20]"
          :size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </FaPageMain>

    <el-dialog v-model="dialogVisible" :title="editForm.id ? '编辑配置' : '新增配置'" width="30%" draggable>
      <el-form :model="editForm" label-width="auto" style="max-width: 600px; margin-top: 10px">
        <el-form-item label="名称">
          <el-input v-model="editForm.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="配置值">
          <el-input v-model="editForm.value" placeholder="请输入配置值"/>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="editForm.description" placeholder="请选择描述"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirm">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="js">
import api from '@/api'
import {toast} from "vue-sonner";

export default {
  created() {
    this.list()
  },
  data() {
    return {
      tableData: [],
      current: 1,
      size: 5,
      total: 50,
      dialogVisible: false,
      selectForm: {
        name: undefined,
        value: undefined,
        description: undefined
      },
      editForm: {
        id: undefined,
        name: undefined,
        value: undefined,
        description: undefined
      }
    }
  },
  methods: {
    list() {
      const pageParams = {
        current: this.current,
        pageSize: this.size,
        ...this.selectForm
      }
      api.post("/config/list", pageParams).then(res => {
        this.tableData = res.data.records
        this.size = res.data.size
        this.current = res.data.current
        this.total = res.data.total
      })
    },
    del(val) {
      api.post("/config/delete", {
        id: val.id
      }).then(res => {
        if (res.code === 0) {
          this.list()
          toast.success(`成功删除编号${val.id}的配置！`)
        } else {
          toast.error(`编号:${val.id}的配置删除失败！`)
        }
      })
    },
    edit(val) {
      this.editForm.id = val.id
      this.editForm.name = val.name
      this.editForm.value = val.value
      this.editForm.description = val.description
      this.dialogVisible = true
    },
    confirm() {
      api.post("/config/saveOrUpdate", this.editForm).then(res => {
        if (res.code === 0) {
          this.list()
          toast.success(`成功更新配置！`)
        } else {
          toast.error(`编号为:${this.editForm.id}的配置编辑失败！`)
        }
      })
      this.dialogVisible = false
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.list()
    },
    handleCurrentChange(val) {
      this.current = val
      this.list()
    }
  }
}
</script>

<style scoped>
.example-pagination-block {
  margin-top: 10px;
}

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
